package com.bawei.module_recommend.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_recommend.R
import com.bawei.module_recommend.databinding.ItemRecommendBinding
import com.bawei.module_recommend.model.CompanyModelItem

/**
 *作者：lxy
 *功能:
 *时间：2025/6/16
 */
class RecommendAdapter(
    private val onItemClick: ((Int) -> Unit)? = null
) : RecyclerView.Adapter<RecommendAdapter.RecommendViewHolder>() {

    private val mData = mutableListOf<CompanyModelItem>()

    fun setData(data: List<CompanyModelItem>) {
        mData.clear()
        mData.addAll(data)
        val size = mData.size
        notifyItemChanged(size - 1, data.size)
    }

    inner class RecommendViewHolder(
        itemView: View,
        private val onItemClick: ((Int) -> Unit)? = null
    ) : RecyclerView.ViewHolder(itemView) {
        private val binding: ItemRecommendBinding? = DataBindingUtil.bind(itemView)
        // 绑定数据到视图
        fun bind(item: CompanyModelItem) {
            binding?.apply {
                // 直接使用数据绑定来设置数据
                companyName.text = "${item.enterpriseName}"
                industryType.text = "行业类型:互联网科技"
                industryCover.text = "行业覆盖:智慧城市解决方案"
                industryAddress.text = "企业所属地区:广州市"
            }
        }

        init {
            itemView.setOnClickListener {
                onItemClick?.invoke(adapterPosition)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecommendViewHolder {
        val binding: ItemRecommendBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.item_recommend,
            parent,
            false
        )
        return RecommendViewHolder(binding.root, onItemClick)
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    override fun onBindViewHolder(holder: RecommendViewHolder, position: Int) {
        val item = mData[position]
        holder.bind(item)
    }
}